﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace CreaMotion.Forms
{
	public partial class DesignForm : Window 
	{
        ObservableCollection<UserControls.TableItem> TablesSource = new ObservableCollection<UserControls.TableItem>();

        public DesignForm()
		{
			this.InitializeComponent();

            btClose.Click += new RoutedEventHandler(btClose_Click);
            listTables.MouseDoubleClick += new MouseButtonEventHandler(listTables_MouseDoubleClick);
            btCreateNhibernateProject.Click += new RoutedEventHandler(btCreateNhibernateProject_Click);
            btOpenRelationshipManager.Click += new RoutedEventHandler(btOpenRelationshipManager_Click);

            listTables.ItemsSource = TablesSource;
        }

        #region DesignTables

        List<Interfaces.Table> DesignTables
        {
            get
            {
                List<Interfaces.Table> tables = new List<Interfaces.Table>();

                foreach (var item in DesignGrid.Children)
                {
                    tables.Add(((UserControls.Table)((ContentControl)item).Content).Source);
                }

                return tables;
            }
        }

        #endregion

        #region Relationship Manager

        void btOpenRelationshipManager_Click(object sender, RoutedEventArgs e)
        {
            RelationshipDesignForm RDF = new RelationshipDesignForm();

            RDF.TableSource = DesignTables;
            RDF.RelationshipSource = DesignSource.Relationships;

            if (RDF.ShowDialog() == RelationshipDesignForm.DialogResult.Close) return;

            //Deleting Old Relationships
            foreach (Interfaces.Relationship oldRelationship in (from Or in designSource.Relationships
                                                                 where Or.FirstTableName == RDF.FirstTable.TableName && Or.SecoundTableName == RDF.SecoundTable.TableName
                                                                 select Or).ToList())
            {
                designSource.Relationships.Remove(oldRelationship);
            }
            
            //Adding New Relationships
            foreach (TempRelationship Relationship in RDF.relationships)
	        {
                Interfaces.Relationship Tmp = new Interfaces.Relationship();

                Tmp.FirstTableName = RDF.FirstTable.TableName;
                Tmp.SecoundTableName = RDF.SecoundTable.TableName;

                Tmp.FirstField = (from t in RDF.FirstTable.Fields
                                  where t.FieldName == Relationship.FirstField.Source.FieldName
                                  select t).First();

                Tmp.FirstField.RelationshipType = Relationship.FirstFieldType;

                Tmp.SecoundField = (from t in RDF.SecoundTable.Fields
                                    where t.FieldName == Relationship.SecoundField.Source.FieldName
                                    select t).First();

                Tmp.SecoundField.RelationshipType = Relationship.SecoundFieldType;

                DesignSource.Relationships.Add(Tmp);
	        }
        }

        #endregion

        void btCreateNhibernateProject_Click(object sender, RoutedEventArgs e)
        {
            if (DesignGrid.Children.Count == 0) { System.Windows.MessageBox.Show("Design is Empty!!"); return; }
            
            using (System.Windows.Forms.FolderBrowserDialog FolderDialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                FolderDialog.Description = "Select a Project Folder";
                FolderDialog.ShowNewFolderButton = true;
                if (FolderDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
                
                NhibernateClassGenerator NCG = new NhibernateClassGenerator();
                NCG.TableSource = DesignTables;
                NCG.DatabaseName = designSource.DatabaseName;
                NCG.RelationshipSource = designSource.Relationships;
                NCG.GeneratorProject(FolderDialog.SelectedPath, textNamespace.Text,(cmbLanguages.SelectedValue as Grid) == VbLanguage ? NhibernateClassGenerator.ProgrammingLanguage.Vb : NhibernateClassGenerator.ProgrammingLanguage.Csharp);
            }
        }

        #region Add Table For DesignForm

        void listTables_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            UserControls.TableItem tmp = listTables.SelectedValue as UserControls.TableItem;
            if (!tmp.IsEnable || tmp.IsAttached) return;

            Layout.DesignerItem tmpContentControl = new Layout.DesignerItem() { MinWidth = 220, MaxWidth=220, MinHeight = 47, Width = 220, Height = 270, HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch };
            UserControls.Table tbl = new UserControls.Table();

            tmpContentControl.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            tmpContentControl.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

            tbl.Height = double.NaN;

            tmpContentControl.Content = tbl;
            tbl.Source = tmp.Source;

            Canvas.SetTop(tmpContentControl, 20);
            Canvas.SetLeft(tmpContentControl, 20);
            
            tmpContentControl.Style = (System.Windows.Style)FindResource("DesignerItemStyle");
            DesignGrid.Children.Add(tmpContentControl);

            tmp.IsAttached = true;
        }

        #endregion

        #region DesignSource

        private Interfaces.Database designSource = null;
        public Interfaces.Database DesignSource
        {
            get
            {
                return designSource;
            }
            set 
            {
                designSource = value;

                labDatabaseName.Text = value.DatabaseName;
                labDatabaseEngine.Text = value.Engine;
                labTableCount.Text = value.Tables.Count.ToString();

                foreach (Interfaces.Table TableItem in value.Tables)
                {
                   TablesSource.Add(new UserControls.TableItem() { Source=TableItem });
                }
            }
        }

        #endregion

        #region Close Form

        void btClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}